﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mDashboard.Common
{
    public class DataTableHelper
    {
        /// <summary>
        /// 合并两个DataTable join
        /// </summary>
        /// <param name="table1"></param>
        /// <param name="table2"></param>
        /// <param name="ColumnsName"></param>
        /// <returns></returns>
        public static DataTable MergeTabel1(DataTable table1, DataTable table2, string ColumnsName)
        {

            for (int i = 0; i < table1.Rows.Count; i++)
            {
                for (int j = 0; j < table2.Rows.Count; j++)
                {
                    if (table1.Rows[i][ColumnsName].ToString() == table2.Rows[j][ColumnsName].ToString())
                    {
                        DataRow[] arrRows = table1.Select("" + ColumnsName + "='" + table1.Rows[i][ColumnsName].ToString() + "'");
                        foreach (DataRow row in arrRows)
                        {
                            decimal alumiumqty = table2.Rows[j]["ALUMINIUM_QTY"].ToString().Length > 0 ? Convert.ToDecimal(table2.Rows[j]["ALUMINIUM_QTY"]) : 0;
                            row["ALUMINIUM_QTY"] = alumiumqty;
                        }
                        break;
                    }
                }

            }
            return table1;
            //var query =
            //     from rHead in table1.AsEnumerable()
            //     from rTail in table2.AsEnumerable()
            //     where rHead.Field<string>("WORK_DATE") == rTail.Field<string>("WORK_DATE")
            //     select new
            //     {
            //         WORK_DATE = rHead.Field<string>("WORK_DATE"),
            //         ALUMINIUM_QTY = rTail.Field<decimal?>("ALUMINIUM_QTY"),
            //         METAL_CABINET_QTY = "",
            //         MC_CONVERT_QTY = "",
            //         WORK_HOURS = "",
            //         PRODUCTIVITY = ""
            //     };
            //foreach (var obj in query)
            //{
            //    dtNew.Rows.Add(obj.WORK_DATE, obj.ALUMINIUM_QTY, obj.METAL_CABINET_QTY, obj.MC_CONVERT_QTY, obj.WORK_HOURS, obj.PRODUCTIVITY);
            //}

        }
        public static DataTable MergeTabel2(DataTable table1, DataTable table2, string ColumnsName)
        {

            for (int i = 0; i < table1.Rows.Count; i++)
            {
                for (int j = 0; j < table2.Rows.Count; j++)
                {
                    if (table1.Rows[i][ColumnsName].ToString() == table2.Rows[j][ColumnsName].ToString())
                    {
                        DataRow[] arrRows = table1.Select("" + ColumnsName + "='" + table1.Rows[i][ColumnsName].ToString() + "'");
                        foreach (DataRow row in arrRows)
                        {
                            decimal metal_cabinetqty = table2.Rows[j]["METAL_CABINET_QTY"].ToString().Length > 0 ? Convert.ToDecimal(table2.Rows[j]["METAL_CABINET_QTY"].ToString()) : 0;
                            row["METAL_CABINET_QTY"] = metal_cabinetqty;
                            row["MC_CONVERT_QTY"] = metal_cabinetqty * 21;
                        }
                        break;
                    }
                }

            }
            return table1;
        }
        public static DataTable MergeTabel3(DataTable table1, DataTable table2, string ColumnsName)
        {

            for (int i = 0; i < table1.Rows.Count; i++)
            {
                for (int j = 0; j < table2.Rows.Count; j++)
                {
                    if (table1.Rows[i][ColumnsName].ToString() == table2.Rows[j][ColumnsName].ToString())
                    {
                        DataRow[] arrRows = table1.Select("" + ColumnsName + "='" + table1.Rows[i][ColumnsName].ToString() + "'");
                        foreach (DataRow row in arrRows)
                        {
                            decimal alumiumqty = row["ALUMINIUM_QTY"].ToString().Length > 0 ? Convert.ToDecimal(row["ALUMINIUM_QTY"]) : 0;
                            decimal mcConvertqty = row["MC_CONVERT_QTY"].ToString().Length > 0 ? Convert.ToDecimal(row["MC_CONVERT_QTY"]) : 0;
                            decimal workhours = table2.Rows[j]["WORK_HOURS"].ToString().Length > 0 ? Convert.ToDecimal(table2.Rows[j]["WORK_HOURS"].ToString()) : 0;
                            row["WORK_HOURS"] = workhours;
                            if (workhours > 0)
                                row["PRODUCTIVITY"] = Math.Round((alumiumqty + mcConvertqty) / workhours, 1);
                        }
                        break;
                    }
                }

            }
            return table1;
        }
    }
}
